﻿/*Page created by Mike Beck -- holds the information for updating the info on VerifyRecipe page*/

using System;
using Microsoft.Practices.ObjectBuilder;
using Common;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.CompositeWeb.Web;

namespace CSC440TeamOne.Admin.Views
{
    public partial class VerifyRecipe : Microsoft.Practices.CompositeWeb.Web.UI.Page, IVerifyRecipeView
    {
        private VerifyRecipePresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.recipeID = Request.Params["Recipe"] == null ? int.MinValue : int.Parse(Request.Params["Recipe"]);
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            repRecipeIngredients.ItemDataBound += new RepeaterItemEventHandler(repRecipeIngredients_ItemDataBound);
        }

        void repRecipeIngredients_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, RecipeIngredient>.Bind(e.Item, a =>
            {
                //Error thrown because a BusinessItem is a recipe (I believe?)
                a.Find<TextBox>("riQuantity").Text = a.BusinessItem.quantityToString();
                a.Find<TextBox>("riMeasurement").Text = a.BusinessItem.measurement;
                a.Find<TextBox>("riIngredient").Text = a.BusinessItem.ingredient.name;
            });
        }

        [CreateNew]
        public VerifyRecipePresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	


        public void show(VerifyRecipeModel Model)
        {
            List<RecipeIngredient> recipeIngredients = Model.Recipe.ingredients;
            if (recipeIngredients.Any())
            {
                repRecipeIngredients.DataSource = recipeIngredients;
                repRecipeIngredients.DataBind();
            }

            this.recipeName.Text = Model.Recipe.name;
            this.recipeInstructions.Text = Model.Recipe.directions;
            this.recipeUser.Text = Model.Recipe.createdBy.FirstName + " " + Model.Recipe.createdBy.LastName;
            this.recipeDate.Text = Model.Recipe.dateSubmitted.Month + "/" + Model.Recipe.dateSubmitted.Day + "/" + Model.Recipe.dateSubmitted.Year;
        }

        public int recipeID
        {
            get
            {
                return ViewState["recipeID"] != null ? (int)ViewState["recipeID"] : int.MinValue;
            }
            set
            {
                ViewState["recipeID"] = value;
            }
        }
    }
}

